﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using ZXing;

namespace mytools
{
    public static class Common
    {
        [DllImport("user32.dll")]
        private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

        private const int SW_RESTORE = 9;
        private const int SW_SHOWMINIMIZED = 2;

        public static void SwitchToProcessWindow(string processName)
        {
            Process[] processes = Process.GetProcessesByName(processName);
            if (processes.Length > 0)
            {
                foreach (Process process in processes)
                {
                    IntPtr mainWindowHandle = process.MainWindowHandle;
                    if (mainWindowHandle != IntPtr.Zero)
                    {
                        Console.WriteLine("有效。");
                        if (IsIconic(mainWindowHandle) != 0) // 如果窗口最小化，则激活窗口
                        {
                            ShowWindow(mainWindowHandle, SW_RESTORE);
                        }
                        else // 如果窗口未最小化，则最小化窗口
                        {
                            ShowWindow(mainWindowHandle, SW_SHOWMINIMIZED);
                        }
                    }
                }
            }
            else
            {
                Console.WriteLine("找不到指定名称的进程。");
            }
        }

        public static bool IsTimestamp(string text)
        {
            // 使用正则表达式检查文本是否为时间戳
            // 这里假设时间戳为 10 位或 13 位数字
            return Regex.IsMatch(text, @"^\d{10}$") || Regex.IsMatch(text, @"^\d{13}$");
        }

        public static DateTime UnixTimeStampToDateTime(double unixTimeStamp)
        {
            // 这里实现将 Unix 时间戳转换为 DateTime 的方法
            DateTime dt = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
            return dt.AddSeconds(unixTimeStamp).ToLocalTime();
        }

        public static string CalculateMD5(string input)
        {
            using (MD5 md5 = MD5.Create())
            {
                byte[] inputBytes = Encoding.UTF8.GetBytes(input);
                byte[] hashBytes = md5.ComputeHash(inputBytes);

                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < hashBytes.Length; i++)
                {
                    sb.Append(hashBytes[i].ToString("x2"));
                }

                return sb.ToString();
            }
        }

        public static string DecodeQRCode(Image image)
        {
            BarcodeReader reader = new BarcodeReader();
            var result = reader.Decode((Bitmap)image);
            //Console.WriteLine("结果");
            //Console.WriteLine(result);
            if (result != null)
            {
                return result.Text;
            }

            return "";
        }

        // 将图片转换为 Base64 字符串
        public static string ImageToBase64(Image image)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                // 将图片以 PNG 格式保存到内存流中
                image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                // 将内存流转换为 byte 数组
                byte[] imageBytes = ms.ToArray();
                // 将 byte 数组转换为 Base64 字符串
                string base64String = Convert.ToBase64String(imageBytes);
                return base64String;
            }
        }

        // 导入Windows API函数
        [DllImport("user32.dll")]
        public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

        [DllImport("user32.dll")]
        public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

        [DllImport("user32.dll")]
        private static extern int IsIconic(IntPtr hWnd);
    }

}
